import java.net.SocketException;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Iterator;
import java.util.SortedSet;

public class RMISpellingClient extends AbstractClient
{

	public RMISpellingClient(String[] the_args) throws SocketException
	{
		super(the_args);
	}

	public static void main(String[] args) throws SocketException
	{
		RMISpellingClient client = new RMISpellingClient(args);
		try
		{
			RemoteSpelling temp1 = (RemoteSpelling) (client.my_registery
					.lookup(args[2]));
			Iterator iterator_word = client.my_searchlist.iterator();
			while (iterator_word.hasNext())
			{
				String wordcheck = (String) iterator_word.next();
				SortedSet<String> tempsuggestions = temp1.check(wordcheck);
				Log.out("Querying service for word " + wordcheck);
				if (tempsuggestions == null)
				{
					Log.out(wordcheck + " is spelled correctly");
				} else
				{
					int num_suggestions = tempsuggestions.size();
					if (num_suggestions > 0)
					{
						StringBuffer sb = new StringBuffer();
						sb.append(wordcheck
								+ "is spelled incorrctly, there are "
								+ num_suggestions + " suggestions");
						Iterator iterator_suggestions = temp1.check(wordcheck)
								.iterator();
						while (iterator_suggestions.hasNext())
						{
							String tempsuggestion = (String) iterator_suggestions
									.next();
							sb.append(' ');
							sb.append(tempsuggestion);
						}
						Log.out(sb.toString());
					} else
					{
						Log.out(wordcheck
								+ " is spelled incorrectly, no suggestions");
					}
				}
			}
			return;
		} catch (AccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e)
		{
			Log.err("Error looking up or  accessing " + e);
			return;
		} catch (NotBoundException e)
		{
			Log.err("Spelling service cannot bound on registery " + e);
			e.printStackTrace();
		}
	}

}
